<?php

/**
 * @author      Nguyen Duc Thuan <me@ndthuan.com>
 * @copyright   Copyright (c) 2009 Nguyen Duc Thuan <me@ndthuan.com>
 */
class Fete_View_Helper_Cycle extends Fete_View_Helper
{
    protected $_currentIndex    = -1;
    protected $_currentValue    = '';
    protected $_values          = array();
    protected $_valueCount      = 0;

    /**
     *
     * @param array $values
     * @return Fete_View_Helper_Cycle
     */
    public function &cycle($values = array())
    {
        if (isset($values[0])) {
            $this->_values      = $values;
            $this->_valueCount  = count($values);
        }
        return $this;
    }

    /**
     *
     * @return Fete_View_Helper_Cycle
     */
    public function &next()
    {
        if (++$this->_currentIndex >= $this->_valueCount) {
            $this->_currentIndex = 0;
        }

        if (isset($this->_values[$this->_currentIndex])) {
            $this->_currentValue = $this->_values[$this->_currentIndex];
        }

        return $this;
    }

    /**
     *
     * @return Fete_View_Helper_Cycle
     */
    public function &prev()
    {
        if (--$this->_currentIndex <= -1) {
            $this->_currentIndex = $this->_valueCount - 1;
        }

        if (isset($this->_values[$this->_currentIndex])) {
            $this->_currentValue = $this->_values[$this->_currentIndex];
        }

        return $this;
    }

    /**
     *
     * @return Fete_View_Helper_Cycle
     */
    public function &reset()
    {
        $this->_currentIndex = -1;

        return $this;
    }

    public function __toString()
    {
        return $this->_currentValue;
    }
}